﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Text;
using System.Web;
 using System.Text.RegularExpressions;
using System.Diagnostics;

namespace WinnerCMS.Common
{
    /// <summary>
    /// 取得Http客户端信息
    /// </summary>
    public class HttpClient
    {

        #region 穿过代理服务器取远程用户真实IP地址
        /// <summary>
        /// 穿过代理服务器取远程用户真实IP地址
        /// </summary>
        /// <returns></returns>
        public static string GetIP()
        {
            return GetIP(HttpContext.Current);
        }

        public static string GetIP(HttpContext current)
        {
            try
            {
                string ip = current.Request.ServerVariables["HTTP_VIA"] != null ? current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] : current.Request.ServerVariables["REMOTE_ADDR"];
                ip = ip.Replace("::1", "127.0.0.1");

                return ip;
            }
            catch
            {
                return "0.0.0.0";
            }
        }
        #endregion

        #region 通过一个URL到文件名
        /// <summary>
        /// 通过一个URL到文件名
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string UrlFileName(string url)
        {
            try
            {
                int index = url.LastIndexOf('/') + 1;//移除前没有用的字符，只保留文件名和后缀
                return url.Remove(0, index);
            }
            catch
            {
                return url;
            }
        }
        #endregion

    }
}